mui.init();

//倒计时  
var countdown=60;  
function settime(val) {
    var num = parseInt(document.getElementById("phoneNum").value);
    //check_mobile(num,val);
    if (countdown == 0) {
        clearInterval(in1);
        val.removeAttribute("disabled");
        val.value="获取验证码";
        countdown = 60;
        return false;
    } else {
        var in1 = setInterval(function () {
            val.setAttribute("disabled", true);
            val.value= countdown + "s后重新发送";
            countdown--;
            if(countdown == 0){
                clearInterval(in1);
                val.removeAttribute("disabled");
                val.value="获取验证码";
                countdown = 60;
                return false;
            }
        },1000)
    }
    fs_code(num);
}


mui.plusReady(function () {
    document.getElementById('submitBtn').addEventListener('tap',function(){
    	var api_host = plus.storage.getItem('HOST_URL');
    	var phoneNum  = document.getElementById('phoneNum').value;
		var reg_verify= document.getElementById('reg_verify').value;
		var newPwd    = document.getElementById('newPwd').value;
	    var rePwd     = document.getElementById('rePwd').value;
   		if(phoneNum== ''){
            mui.toast('手机号不能为空',{ duration:'2000', type:'div'});
			return false;
		}else if(reg_verify== ''){
            mui.toast('验证码不能为空',{ duration:'2000', type:'div'});
			return false;
		}else if(newPwd== ''){
	   		plus.ui.toast('新密码不能为空');
	   		return false;
	   	}else if(rePwd == ''){
	   		plus.ui.toast('确认密码不能为空');
	   		return false;
	   	}else if(newPwd !== rePwd){
	   		plus.ui.toast('两次密码不一致');
	   		return false;
	   	}else{
            plus.nativeUI.showWaiting('密码重置中...');
            mui.ajax({
                url:api_host+'/api/linxuan/user/forgetpwd',
                data:{'mobile':phoneNum,'verify':reg_verify,'newpwd':newPwd,'repwd':rePwd},
                dataType:'json',
                type:'post',
                timeout:5000,
                success:function(msg){
                    if(msg.code == 1){
                        plus.nativeUI.closeWaiting();
                        mui.toast(msg.msg,{ duration:'1500', type:'div'});
                        setTimeout(function () {
                            mui.openWindow({
                                url: "login.html",
                                id:'loginHtml',
                            })
                        },2000)
                    }else{
                        plus.nativeUI.closeWaiting();
                        mui.toast(msg.msg,{ duration:'2000', type:'div'});
                    }
                },
                error:function(xhr,type,errorThrown){
                    plus.nativeUI.closeWaiting();
                    mui.toast('重置密码出错，请联系客服！',{ duration:'2000', type:'div'});
                }
            });
		}

    });
   
});

//发送验证码
function fs_code(num) {
    var api_host = plus.storage.getItem('HOST_URL');
    mui.ajax({
        url:api_host+'/api/linxuan/user/sendverify',
        data:{'account':num},
        dataType:'json',
        type:'post',
        timeout:10000,
        success:function(msg){
            if(msg.code == 1){
                mui.toast(msg.msg,{ duration:'1000', type:'div'});
            }else{
                mui.toast(msg.msg,{ duration:'1000', type:'div'});
            }
        },
        error:function(xhr,type,errorThrown){
            mui.toast('系统错误，请联系客服！',{ duration:'2000', type:'div'});
        }
    });
}
